import Vue from 'vue'
import Router from 'vue-router'
import Login from '../views/backend/Login'
import axios from "axios";

Vue.use(Router)
const router = new Router({
    routes: [

      {
        path: '/admin/login',
        name: 'Login',
        component: Login
      },
      //前台  start
      {
        path:'/front/ActiveInfo',
        component: ()=>import('../views/front/ActiveInfo')
      },
      {
        path:'/front/MyInfo',
        component: ()=>import('../views/front/MyInfo')
      },
      {
        path:'/front/ActiveSearch',
        component: ()=>import('../views/front/ActiveSearch')
      },
      {
        path:'/front/NewsDetail',
        component: ()=>import('../views/front/NewsDetail')
      },
      {
        path:'/front/News',
        component: ()=>import('../views/front/News')
      },
      {
        path:'/front/AllNews',
        component: ()=>import('../views/front/AllNews')
      },
      {
        path:'/front/DreamService',
        component: ()=>import('../views/front/DreamService')
      },
      {
        path:'/front/SecondClass',
        component: ()=>import('../views/front/SecondClass')
      },
      {
        path:'/front/MyTribe',
        component: ()=>import('../views/front/MyTribe'),
        children:[
          {
            path:'/front/AllTribe',

            meta:{
              title:'全部部落'
            },
            component:() => import('../views/front/AllTribe'),
          },
          {
            path: '/front/MyJoinTribe',
            name: 'MyJoinTribe',
            meta:{
              title:'我加入的部落'
            },
            component: ()=>import('../views/front/MyJoinTribe')
          },
        ]
      },
      {
        path:'/front/MyActive',
        component: ()=>import('../views/front/MyActive')
      },
      // {
      //   path:'/',
      //   component: ()=>import('../views/front/Home')
      // },
      {
        path:'/front/Home',
        component: ()=>import('../views/front/Home')
      },


      {
        path: '/front/TribalDetails',
        name: 'TribalDetails',
        component: ()=>import('../views/front/TribalDetails')
      },
      {
        path: '/front/TribalDetailsManage',
        name: 'TribalDetailsManage',
        component: ()=>import('../views/front/TribalDetailsManage')
      },
      //前台  end


    //   超级管理员

      {
        path:'/admin/Main',
        name:'main',
        component: () => import('../views/Main'),
        children: [
          {
            path:'/admin/Tribe',
            name:'Tribe',
            component:() => import('../views/backend/Tribe'),
          },

          {
            path:'/admin/ActivityApply',
            name:'Tribe',
            component:() => import('../views/main/ActivityApply'),
          },
          {
            path:'/admin/TribeApply',
            name:'TribeApply',
            component:() => import('../views/backend/TribeApply'),
          },

        ]
      },


      //   活动管理员
      {
        path:'/admin/HomePage',
        name:'HomePage',
        component: () => import('../views/HomePage'),

        children: [
          {
            path:'/admin/Activity',
            name:'Activity',
            component:() => import('../views/backend/Activity'),

          },
          {
            path:'/admin/AApply',
            name:'AApply',
            component:() => import('../views/backend/AApply'),
          },
          {
            path:'/admin/GiveScore',
            name:'GiveScore',
            component:() => import('../views/homepage/GiveScore'),
          },
        ]
      },


    ],
});
//让页面跳转后在页面顶部
// router.beforeEach((to,from,next)=>{
//   window.scrollTo(0,0);
//   next();
// });

router.beforeEach((to, from, next) => {
  if (to.path.startsWith('/admin')) {
    //如果是登录路由则直接放行
    if (to.path == '/admin/login' ) return next()
    //从sessionStorage取得token代表jwt长串
    var token = sessionStorage.getItem("token");
    if (!token) {//token不存在,代表未登录
      return next('/admin/login')//跳转到登录页面
    } else {//token存在,代表登录
      //检验jwt的合法性
      axios({
        url: 'http://localhost:9257/admin/checkToken',
        method: 'get',
        headers: {
          token:token //请求头加token:jwt长串
        }
      }).then(resp => {
        if (!resp.data) {
          next({path: '/error'})
        }
      })
      next()//放行
    }
  } else {
    /* next({path: '/error'})*/
    if (to.path.startsWith('/front')) return next()  //如果是登录路由则直接放行
    //从sessionStorage取得token代表jwt长串
    var token = sessionStorage.getItem("token");
    if (!token) {//token不存在,代表未登录
      return next('/admin/login')//跳转到登录页面
    } else {//token存在,代表登录
      //检验jwt的合法性
      axios({
        url: 'http://localhost:9257/admin/checkToken',
        method: 'get',
        headers: {
          token: token //请求头加token:jwt长串
        }
      }).then(resp => {
        if (!resp.data) {
          next({path: '/error'})
        }
      })
      next()//放行
    }
  }
})

export default router
